package com.a3dweijing.wetouch2.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.a3dweijing.wetouch2.R;
import com.a3dweijing.wetouch2.Util.EvenBusUtil;
import com.a3dweijing.wetouch2.base.App;
import com.a3dweijing.wetouch2.bean.EvenMsg;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.a3dweijing.wetouch2.constant.AppKey.WEIXINGKEY;

public class WXPayEntryActivity extends Activity implements IWXAPIEventHandler
{
    
    @BindView(R.id.imageview)
    ImageView imageview;
    
    @BindView(R.id.SorF)
    TextView SorF;
    
    @BindView(R.id.button)
    Button button;
    
    private IWXAPI api;
    
    private int errcode;
    
    /**
     * 微信支付区分页面
     */
    public static final String BUY = "BUY";
    
    public static final String BUYVIP = "BUYVIP";
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wxpay_entry);
        ButterKnife.bind(this);
        api = App.getWxapi();
        this.api.handleIntent(this.getIntent(), this);
    }
    
    @Override
    protected void onNewIntent(Intent intent)
    {
        super.onNewIntent(intent);
        this.setIntent(intent);
        api.handleIntent(intent, this);
    }
    
    @Override
    public void onReq(BaseReq baseReq)
    {
        
    }
    
    @Override
    public void onResp(BaseResp baseResp)
    {
        if (baseResp.errCode == 0)
        {
            this.errcode = baseResp.errCode;
            imageview.setImageDrawable(getResources().getDrawable(R.drawable.success));
            SorF.setText("支付成功");
            SorF.setTextColor(getResources().getColor(R.color.lvse));
            button.setBackground(getResources().getDrawable(R.drawable.sbmint_lvse));
        }
        else
        {
            this.errcode = baseResp.errCode;
            imageview.setImageDrawable(getResources().getDrawable(R.drawable.fail));
            SorF.setText("支付失败");
            SorF.setTextColor(getResources().getColor(R.color.text_red_a4));
            button.setBackground(getResources().getDrawable(R.drawable.sbmint_hongse));
        }
    }
    
    @Override
    protected void onDestroy()
    {
        if (errcode == 0)
        {
            EvenMsg evenMsg = new EvenMsg(EvenMsg.WXPAYSUCCESS, null);
            EvenBusUtil.post(evenMsg);
        }
        else
        {
            EvenMsg evenMsg = new EvenMsg(EvenMsg.WXPAYFAIL, null);
            EvenBusUtil.post(evenMsg);
        }
        super.onDestroy();
    }
    
    @OnClick(R.id.button)
    public void onViewClicked()
    {
        finish();
    }
}
